package com.hxht.cmp.service.impl;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.CourtRoom;
import com.hxht.cmp.entity.Plan;
import com.hxht.cmp.mapper.CourtRoomMapper;
import com.hxht.cmp.mapper.PlanMapper;
import com.hxht.cmp.service.CourtStatusService;
import com.hxht.cmp.utils.XmlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * 法庭状态
 */
@Service
public class CourtStatusServiceImpl implements CourtStatusService {
    @Autowired
    private CourtRoomMapper courtRoomMapper;
    @Autowired
    private PlanMapper planMapper;

    @Override
    public List<Map<String, Object>> selectCourtStatusList(String courtUid) {
        try {
            List<CourtRoom> courtRoomList = Constants.COURTROOM_LIST;
            //查看当前法院下的法庭是否有开庭信息
            List<Plan> plans = courtRoomMapper.selectStatusList(courtUid);
            LinkedList<Map<String, Object>> mapList = new LinkedList<>();
            for (CourtRoom courtRoom : courtRoomList) {
                //获取当前法院下的法庭信息
                if (courtRoom.getCourtUid().equals(courtUid)) {
                    Map<String, Object> map = new LinkedHashMap<>();
                    map.put("courtUid", courtRoom.getCourtUid());
                    map.put("courtRoomName", courtRoom.getName());
                    map.put("courtroomUid", courtRoom.getUid());
                    map.put("status", "空闲");
                    map.put("deviceIp", courtRoom.getDeviceIp());
                    map.put("deviceType", courtRoom.getDeviceType());
                    //匹配相应的法庭
                    for (Plan plan : plans) {
                        //案件信息对应的法庭
                        if (plan.getCourtRoomUid().equals(courtRoom.getUid())) {
                            map.put("caseUid",plan.getCaseUid());
                            if (plan.getStatus() == 0) {
                                map.put("status", "即将开庭");
                            } else if (plan.getStatus() == 1) {
                                map.put("status", "正在开庭");
                            } else if (plan.getStatus() == 2) {
                                map.put("status", "休庭");
                            }
                        }
                    }
                    //解析获取到的XML数据
                    if (!ObjectUtils.isEmpty(courtRoom.getCfg())) {
                        List<Map<String, String>> cfgList = XmlUtils.readRecord(courtRoom.getCfg(), courtRoom.getDeviceIp());
                        map.put("cfg", cfgList);
                    }
                    mapList.addLast(map);
                }
            }
            Collections.sort(mapList, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    System.out.println(o1.get("courtRoomName") + "\t" + o2.get("courtRoomName"));
                    String status = o1.get("status").toString();
                    String status2 = o2.get("status").toString();
                    if ("空闲".equals(status) && "即将开庭".equals(status2)) {
                        return 1;
                    } else if ("即将开庭".equals(status) && "空闲".equals(status2)) {
                        return 1;
                    } else if ("空闲".equals(status2) && "空闲".equals(status)) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            });
            return mapList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> selectVideoStatus(String courtUid) {
        Plan plan = new Plan();
        plan.setStatus(Byte.parseByte("1"));
        plan.setCourtUid(courtUid);
        List<Plan> planList = planMapper.select(plan);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (!ObjectUtils.isEmpty(planList)) {
            for (Plan p : planList) {
                Map<String, Object> map = new HashMap<>();
                CourtRoom courtRoom = Constants.COURTROOM_MAP.get(p.getCourtRoomUid());
                map.put("courtname", courtRoom.getName());
                map.put("courtId", p.getCourtRoomUid());
                map.put("planId", p.getUid());
                map.put("courtroomUid", courtRoom.getUid());
                if (!ObjectUtils.isEmpty(courtRoom.getCfg())) {
                    String cfg = courtRoom.getCfg();
                    //解析获取到的XML数据
                    List<Map<String, String>> cfgList = XmlUtils.readRecord(cfg, courtRoom.getDeviceIp());
                    map.put("cfg", cfgList);
                }
                mapList.add(map);
            }
            return mapList;
        }
        return null;
    }
}